const app = getApp()

import form_submit from '../../mixins/form_submit.js'
import { HiddenCall } from '../../request/api'
const request = new HiddenCall
import { loginExit, updatePassword } from "../../utils/http.js"
import { testPhone } from "../../utils/method.js"
var countdown = 60
var time = null
Page({
  /**
    *  修改密码
    * - @author           王磊
    * - @date             2018-12-02
    * firstPassword     输入的新密码
    * lastPassword      再次输入的密码
    * isPhone           手机号验证
    */
  mixins: [form_submit],
  data: {
    firstPassword: '',
    lastPassword: '',
    isPassworld: true,
    isLastPassworld: null,
    testCodeText: '获取验证码',
    isIcon: false,
    isIconTwo: false,
    isPhone: true
  },
  onUnload () {
    countdown = 60
    clearTimeout(time)
    time = null
  },
  /**
   * 输入新密码不够6位提示
   * @author wl
   */
  getPassword(e) {
    this.setData({ firstPassword: e.detail.value })
    if (this.data.firstPassword===this.data.lastPassword) {
      this.setData({isLastPassworld: 1})
    }
  },
  /**
   * 输入新密码不够6位提示
   * @author wl
   */
  passWordPrompt(e) {
    this.setData({ firstPassword: e.detail.value })
    // 校验密码
    if (this.data.firstPassword) this.setData({isPassworld: this.testPassword(this.data.firstPassword)})
  },
   /**
   * 失焦验证手机号
   * @author wl
   * @date 2019-3-12
   */
  testPassword (value) {
    let reg = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/
    return reg.test(value)
  },
  lastPassword (e) {
    this.setData({ lastPassword: e.detail.value })
    if (this.data.firstPassword===this.data.lastPassword) this.setData({isLastPassworld: 1})
  },
  /**
   * 获取再次输入的密码
   * @author wl
   */
  getLastPassword(e) {
    if (!this.data.lastPassword) return
    // 校验密码
    if (this.data.firstPassword===this.data.lastPassword) {
      this.setData({isLastPassworld: 1})
    } else {
      this.setData({isLastPassworld: 2})
    }
  },
  /**
  * 保存密码
  * @author wl
  */
  savePassword() {
    let that = this
    // 密码大于等于6位
    if ((this.data.firstPassword === this.data.lastPassword) && this.data.isPassworld) {
      // 判断输入密码是否小于6位
      this.setData({ btnLoading: true })
      updatePassword({ authCode: this.data.codeText, workPhone : this.data.phoneNumber }, app.globalData.uuid).then((res) => {
        if (res.statusCode === 200) {
          app.globalData.Authorization = res.result.token
          request.modifyPassword({ newPassword: this.data.lastPassword }).then(() => {
            wx.showToast({
              title: '密码已更新~',
              icon: 'none',
              success() {
                that.setData({ btnLoading: false })
                setTimeout(() => {
                  wx.switchTab({
                    url: '../index/index'
                  })
                }, 500)
              }
            })
            that.setData({ isCode: true })
          })
        } else {
          res.message?wx.showToast({ title: res.message, icon: 'none' }):''
          that.setData({ btnLoading: false })
          res.statusCode===400?that.setData({isCode: false}):that.setData({isPhone: false})
        }
      })
    } else this.setData({isLastPassworld: 2})
  },
  /*
  * 获取手机号
  */
  getPhone (e) {
    this.setData({
      phoneNumber: e.detail.value
    })
  },
  /**
   * 调用验证码接口
   */
  testCode () {
    this.data.phoneNumber?request.getTestCode(this.data.phoneNumber).then((res) => {
      if (res.statusCode === 200) this.settime()
    }) : wx.showToast({ title: '请输入手机号', icon: 'none'})
    
  },
  /**
   * 获取验证码
   */
  getCode (e) {
    this.setData({ codeText: e.detail.value})
  },
  /**
   * 60秒倒计时
   */
  settime() {
    let that = this
    if (countdown == 0) {
      this.setData({ testCodeText: '获取验证码'})
      countdown = 60
      return
    }
    else {
      this.setData({ testCodeText: `${countdown}后重新获取` })
      countdown--
    }
    time = setTimeout(function () {
      that.settime()
    }, 1000)
  },
  /*
  * 显示密码
  */
  showPassword() {
    this.setData({ isIcon: !this.data.isIcon })
  },
  showPasswordTwo() {
    this.setData({ isIconTwo: !this.data.isIconTwo })
  },
  /**
   * 失焦验证手机号
   * @author wl
   * @date 2019-3-12
   */
  blurPhone () {
    let isPhone = this.data.phoneNumber?testPhone(this.data.phoneNumber, 'is'):false
    if (this.data.phoneNumber) this.setData({ isPhone })
  }
})